package com.paul.sys.service.impl;
import com.paul.sys.mapper.SymenuMapper;
import com.paul.sys.model.Symenu;
import com.paul.sys.service.SymenuService;
import com.paul.sys.tools.CommPageBean;
import com.paul.sys.tools.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class SymenuServiceImpl implements SymenuService{
    @Autowired
    SymenuMapper symenuMapper;

    /*
    该方法为用户登录获取菜单专用
     */
    @Override
    public Map listPrivateByUserId(Map<String, String> param) {
        Map map = new HashMap();
        List<Symenu> list =  symenuMapper.listPrivateByUserId(param);
        List<String> menus = new ArrayList<>();
        for(Symenu menu:list){
            param.put("menulevel","2");
            param.put("punid",menu.getUnid()+"");
            List<Symenu>  list_1 =  symenuMapper.listPrivateByUserId(param);
            if(menu.getUrl()!=null&&menu.getUrl().trim().length()!=0){
                menus.add(param.get("path")+menu.getUrl());
            }
            menu.setChild(list_1);
            for(Symenu menu_1:list_1){
                param.put("menulevel","3");
                param.put("punid",menu_1.getUnid()+"");
                if(menu_1.getUrl()!=null&&menu_1.getUrl().trim().length()!=0){
                    menus.add(param.get("path")+menu_1.getUrl());
                }
                List<Symenu>  list_2 =  symenuMapper.listPrivateByUserId(param);
                menu_1.setChild(list_2);
            }
        }
        map.put("menus",list);
        map.put("session",menus);

        return map;
    }
    @Override
     public List<Symenu> listPublicAll(Map<String, String> param) {
        return symenuMapper.listPublicAll(param);
    }

    @Override
    public List<Symenu> listPrivateAll(Map<String, String> param) {
        return symenuMapper.listPrivateAll(param);
    }

    @Override
    public List<Symenu> listByPunid(Map<String,String> param){
        return symenuMapper.listByPunid(param);
    }

    @Override
    public Symenu selectByUnid(String unid) {
        return symenuMapper.selectByUnid(unid);
    }

    @Override
    public PageData<Symenu> listByPage(Map<String, String> param) {
        PageData<Symenu> pageData = new PageData<Symenu>();
        CommPageBean pageBean = CommPageBean.getPageBeanByParam(param);
        List<Symenu> list = symenuMapper.listByPage(param,pageBean);
        pageData.setPageBean(pageBean);
        pageData.setListData(list);
        return pageData;
    }
    @Override
    public List<Map> getAllTree() {
        List<Map> list = symenuMapper.getAllTree();
        return list;
    }

    @Override
    public int updateByPrimaryKey(Symenu symenu) {
        return symenuMapper.updateByPrimaryKey(symenu);
    }

    @Override
    public int insert(Symenu symenu) {
        return symenuMapper.insert(symenu);
    }

    @Override
    @Transactional
    public int deleteMenutree(String unid) {
        return deleteNode(unid);
    }

    @Override
    public List<Map> getRoleLink(String unid) {
        return this.symenuMapper.getRoleLink(unid);
    }

    @Override
    @Transactional
    public int linkRole(Map<String, Object> param) {
     return linkRoleRec(param);
    }

    @Override
    @Transactional
    public int unlinkRole(Map<String, Object> param) {
       return unlinkRoleRec(param);
    }

    /**
     * 递归关联角色菜单关系
     * @param param
     * @return
     */
    private int linkRoleRec(Map<String, Object> param){
        List list= this.symenuMapper.selectRoleLink(param);
        Map<String, Object> cparam=new HashMap<String, Object>();
        Symenu symenu=symenuMapper.selectByUnid(param.get("menuunid").toString());
        if(symenu!=null){
            cparam.putAll(param);
            cparam.replace("menuunid", symenu.getPunid());
            linkRoleRec(cparam);
            if(list.size()>0){
                this.symenuMapper.updateRoleLinkEnable(param);
            }
            else{
                this.symenuMapper.insertRoleLink(param);
            }
        }
        return 1;
    }

    /**
     * 递归解除角色菜单关系
     * @param param
     * @return
     */
    private int unlinkRoleRec(Map<String, Object> param){
        List list= this.symenuMapper.selectRoleLink(param);
        Map<String,String> ppram=new HashMap<String,String>();
        Map<String, Object> cparam=new HashMap<String, Object>();

        ppram.put("punid",param.get("menuunid").toString());
        List<Symenu> menutrees=this.symenuMapper.listByPunid(ppram);
        for(int i=0;i<menutrees.size();i++){
            cparam.putAll(param);
            cparam.replace("menuunid",menutrees.get(i).getUnid());
            unlinkRoleRec(cparam);
        }
        int res=0;
        if(list.size()>0){
            this.symenuMapper.updateRoleLinkEnable(param);
            res=1;
        }
        else{
            res=0;
        }
        return res;
    }

    /**
     * 递归删除节点
     * @param unid
     * @return
     */
    private int deleteNode(String unid){
        Map param = new HashMap();
        param.put("punid",unid);
        List<Symenu> list=this.symenuMapper.listByPunid(param);
        int n=0;
        if(list.size()>0) {
            for(int i=0;i<list.size();i++){
                this.deleteNode(list.get(i).getUnid());
            }
        }
        n=this.symenuMapper.delete(unid);
        return n;
    }
}
